/*会议审批插件*/

// 待审批会议列表URL
var bookedConfTableUrl;

// 会议列表URL
var confTableUrl;

// 相关会议列表URL
var detailedConfUrl;

// 处理会议URL
var dealConfUrl;

// 会议日历控件ID
var confCalendarId;

// 待审批会议列表
var bookedConfTableId;

// 会议列表
var confTableId;

// 相关会议列表
var detailedConfTable;

// 当前时间
var currentTime;

// 起始时间
var startTime;

// 终止时间
var endTime;

// 当前选中会议室Id
var selectBoradroomId;

// 当前选中待审批会议列表行号
var bookedSelectedRow;

// 当前选中会议显示列表行号
var confSelectedRow;

// 当前选中待审批会议数据
var bookedSelectedData;

// 当前选中会议显示数据
var confSelectedData;

//具体审批显示数据
var detailedConfData;

// 批复处理对话框
var approvalDialog;

// 消息对话框
var messageDialog;

/**
 * 设置待审批会议列表URL
 */
function setBookedConfTableUrl(url) {
    bookedConfTableUrl = url;
}

/**
 * 设置会议列表URL
 */
function setConfTableUrl(url) {
    confTableUrl = url;
}

/**
 * 设置相关会议列表URL
 */
function setDetailedConfUrl(url) {
    detailedConfUrl = url;
}

/**
 * 设置处理会议URL
 */
function setDealConfUrl(url) {
    dealConfUrl = url;
}

/**
 * 设置当前会议室id
 */
function setCurBoradroomId(id) {
    var values = $(id).val().split(":");
    selectBoradroomId = values[0];
    if (values[1] == 1) {
        $(id).addClass("approval_broadroom");
    } else {
        $(id).removeClass("approval_broadroom");
    }
}

/**
 * 设置待审批会议列表
 */
function setBookedConfTable(id) {
    bookedConfTableId = id;

    // 待审批会议列表行选中事件
    $(bookedConfTableId).bind("rowselected", function (event, data, row) {
        if (data != null) {

            // 取得选中会议室id
            selectBoradroomId = data.boardroomId;

            // 取得选中会议id
            conferenceId = data.id;

            // 根据当前会议时间计算起始时间和终止时间
            currentTime = new Date(data.startTime.replace(/-/ig, '/'));
            startTime = getStartTime(currentTime);
            endTime = getEndTime(currentTime);

            // 设置当前选中待审批会议列表行号
            bookedSelectedRow = row;

            // 设置当前选中待审批会议列表数据
            bookedSelectedData = data;

            // 设置会议室导航栏
            $("#boardroom").val($("#boardroom").find("#" + selectBoradroomId).val());
            confTableData = getConfInfo(confTableUrl);

            // 设置会议日历
            $(confCalendarId).setNavigateDate({
                startTime: startTime,
                endTime: endTime
            });
            $(confCalendarId).drewWeekCalendar({data: confTableData});

            // 初始化会议显示列表
            createConfTable(confTableData);

            // 设置会议显示列表选中行
            confSelectedRow = getSelectedRow(confTableId, conferenceId);
            confSelectedData = data;
            $(confTableId).setSelectedRow({
                rowNumber: confSelectedRow
            });
            $(confCalendarId).selectedConference({data: confSelectedData});
            detailedConfData = getConfDetailedConfData(detailedConfUrl, conferenceId);
            createDetailedConfTable(detailedConfData);
        }
    });
}

/**
 * 设置会议列表
 */
function setConfTable(id) {
    confTableId = id;

    // 会议显示列表行选中事件
    $(confTableId).bind("rowselected", function (event, data, row) {
        if (data != null) {
            conferenceid = data.id;

            // 刷新待审批会议列表
            bookedSelectedRow = null;
            bookedSelectedData = null;
            $(bookedConfTableId).setSelectedRow({
                rowNumber: bookedSelectedRow
            });

            // 设置当前选中会议显示列表行号
            confSelectedRow = row;

            // 设置当前选中会议显示列表数据
            confSelectedData = data;
            $(confCalendarId).selectedConference({data: confSelectedData});
            detailedConfData = getConfDetailedConfData(detailedConfUrl, conferenceid);
            createDetailedConfTable(detailedConfData);
        }
    });
}

/**
 * 设置会议室日历
 * @param id
 */
function setConfCalendar(id) {
    confCalendarId = id;

    // 点击前一周时间事件
    $(confCalendarId).bind("selectPrevDate", function (event, time1, time2) {
        startTime = time1;
        endTime = time2;
        refreshConfTable();

    });

    // 点击后一周时间事件
    $(confCalendarId).bind("selectNextDate", function (event, time1, time2) {
        startTime = time1;
        endTime = time2;
        refreshConfTable();
    });
}

/**
 * 设置相关会议
 */
function setDetailedConfTable(id) {
    detailedConfTable = id;
}

/**
 * 设置会议审批对话框
 */
function setApprovalDialog(id) {
    approvalDialog = id;
    $(approvalDialog).initCustomDialog({
        title: "审批会议",
        width: 600,
        height: 580,
        button1: "批准会议",
        button2: "撤销批准",
        button3: "拒绝会议",
        buttonSize: 3,
        modal: true
    });
    $(approvalDialog).bind("btnClick", function (event, name, value) {
        values = value.split(":");
        if (values[1] == 1 || values[1] == 3 || values[1] == 5) {
            if ($(approvalDialog).find("#remark").val() == null || $(approvalDialog).find("#remark").val() == "") {
                $(approvalDialog).find("#remarkWarning").show();
            } else {
                $(approvalDialog).dialog("close");
                var remark = $(approvalDialog).find("#remark").val();
                var result = dealConf(dealConfUrl, values[0], values[1], remark);
                if (result != null && result != "ERROR_RES_02") {
                    $(messageDialog).openMessageDialog({message: "操作成功"});
                    refreshConfTable();
                    detailedConfData = getConfDetailedConfData(detailedConfUrl, result);
                    createDetailedConfTable(detailedConfData);
                } else {
                    $(messageDialog).openMessageDialog({message: "操作失败,CODE:" + result});
                }
            }
        } else {
            $(approvalDialog).dialog("close");
            var result = dealConf(dealConfUrl, values[0], values[1], $(approvalDialog).find("#remark").val());
            if (result != null && result != "ERROR_RES_02") {
                $(messageDialog).openMessageDialog({message: "操作成功"});
                refreshConfTable();
                detailedConfData = getConfDetailedConfData(detailedConfUrl, result);
                createDetailedConfTable(detailedConfData);
            } else {
                $(messageDialog).openMessageDialog({message: "操作失败,CODE:" + result});
            }
        }
    });
}

/**
 * 设置消息对话框
 * @param id
 */
function setMessageDialog(id) {
    messageDialog = id;
    $(messageDialog).messagedialog({
        title: "提示",
        type: 0,
        width: 400,
        height: 300,
        modal: true
    });
}

/**
 * 初始化会议审批JS
 */
function initConfApproval(interval) {
    currentTime = new Date();
    startTime = getStartTime(currentTime);
    endTime = getEndTime(currentTime);
    if (interval == null || interval.length == 0) {
        interval = 5000;
    }
    $(confCalendarId).setNavigateDate({
        startTime: startTime,
        endTime: endTime
    });
    data = getBookConfInfo(bookedConfTableUrl);
    createBookedConfTable(data);
    data = getConfInfo(confTableUrl);
    createConfTable(data);
    $(confCalendarId).drewWeekCalendar({data: data});
    setInterval(function () {
        data = getBookConfInfo(bookedConfTableUrl);
        createBookedConfTable(data);
        if (bookedSelectedData != null) {
            bookedSelectedRow = getSelectedRow(bookedConfTableId, bookedSelectedData.id);
            if (bookedSelectedRow != null && bookedSelectedRow != "") {
                $(bookedConfTableId).setSelectedRow({
                    rowNumber: bookedSelectedRow
                });
            } else {
                bookedSelectedData = null;
            }
        }
        data = getConfInfo(confTableUrl);
        createConfTable(data);
        $(confCalendarId).drewWeekCalendar({data: data});
        if (confSelectedData != null) {
            confSelectedRow = getSelectedRow(confTableId, confSelectedData.id);
            if (confSelectedRow != null && confSelectedRow != "") {
                $(confTableId).setSelectedRow({
                    rowNumber: confSelectedRow
                });
                $(confCalendarId).selectedConference({data: confSelectedData});
                data = getConfDetailedConfData(detailedConfUrl, confSelectedData.id);
                createDetailedConfTable(data);
            } else {
                confSelectedData = null;
                createDetailedConfTable(null);
            }
        }
    }, interval);
}

/**
 * 选择会议室
 */
function selectBoardroom(id) {
    var values = $(id).val().split(":");
    selectBoradroomId = values[0];
    if (values[1] == 1) {
        $(id).addClass("approval_broadroom");
    } else {
        $(id).removeClass("approval_broadroom");
    }
    refreshConfTable();
}

/**
 * 实时获取待审批会议数据
 */
function getBookConfInfo(url) {
    try {
        var obj = $.ajax({
            url: url,
            async: false,
            timeout: 60000
        }).responseText;
        return $.parseJSON(obj);
    } catch (e) {
        return null;
    }
}

/**
 * 实时获取会议显示数据
 */
function getConfInfo(url) {
    try {
        var obj = $.ajax({
            url: url,
            async: false,
            timeout: 60000,
            data: {boardroomId: selectBoradroomId, startTime: startTime, endTime: endTime}
        }).responseText;
        return $.parseJSON(obj);
    } catch (e) {
        return null;
    }
}

/**
 * 实时获取相关会议介绍数据
 */
function getConfDetailedConfData(url, id) {
    try {
        var obj = $.ajax({
            url: url,
            async: false,
            timeout: 60000,
            data: {id: id}
        }).responseText;
        return $.parseJSON(obj);
    } catch (e) {
        return null;
    }
}

/**
 * 进行提交、审批、取消操作
 */
function dealConf(url, id, status, remark) {
    var obj = $.ajax({
        url: url,
        async: false,
        timeout: 60000,
        data: {id: id, status: status, remark: remark}
    }).responseText;
    return obj;
}

/**
 * 创建待审批会议列表
 */
function createBookedConfTable(data) {
    if ($(bookedConfTableId).find(".middle_2_bottom_table_1") != null) {
        $(bookedConfTableId).find(".middle_2_bottom_table_1").remove();
    }
    if (data != null) {
        for (var i = 0; i < data.length; i++) {
            $(bookedConfTableId).append("<tr class='middle_2_bottom_table_1' ></tr>");
            $(bookedConfTableId).find("tr").eq(i + 1).append("<td>" + (i + 1) + "<input type='hidden' value='" + data[i].id + "'/>" + "</td>");
            $(bookedConfTableId).find("tr").eq(i + 1).append("<td>" + data[i].conferenceTime + "</td>");
            $(bookedConfTableId).find("tr").eq(i + 1).append("<td>" + data[i].meetingName + "</td>");
            $(bookedConfTableId).find("tr").eq(i + 1).append("<td>" + data[i].boardroom + "</td>");
            $(bookedConfTableId).find("tr").eq(i + 1).append("<td>" + data[i].flag + "</td>");
        }
    }
    $(bookedConfTableId).initDataTable({
        rowSelected: true,
        data: data
    });
}

/**
 * 创建会议列表
 */
function createConfTable(data) {
    if ($(confTableId).find(".middle_2_bottom_table_1") != null) {
        $(confTableId).find(".middle_2_bottom_table_1").remove();
    }
    if (data != null) {
        for (var i = 0; i < data.length; i++) {
            $(confTableId).append("<tr class='middle_2_bottom_table_1'></tr>");
            $(confTableId).find("tr").eq(i + 1).append("<td style='border-left: #818080 solid 1px;'>" + (i + 1) + "<input type='hidden' value='" + data[i].id + "'/>" + "</td>");
            $(confTableId).find("tr").eq(i + 1).append("<td>" + data[i].conferenceTime + "</td>");
            $(confTableId).find("tr").eq(i + 1).append("<td>" + data[i].meetingName + "</td>");
            $(confTableId).find("tr").eq(i + 1).append("<td>" + data[i].boardroom + "</td>");
            $(confTableId).find("tr").eq(i + 1).append("<td>" + data[i].stafferCount + "</td>");
            $(confTableId).find("tr").eq(i + 1).append("<td>" + data[i].flag + "</td>");
            if (data[i].status == "0") {
                $(confTableId).find("tr").eq(i + 1).append("<td>待审批</td>");
            } else if (data[i].status == "1" || data[i].status == "3") {
                $(confTableId).find("tr").eq(i + 1).append("<td>已预订</td>");
            } else if (data[i].status == "2" || data[i].status == "4") {
                $(confTableId).find("tr").eq(i + 1).append("<td>已审批</td>");
            } else if (data[i].status == "5") {
                $(confTableId).find("tr").eq(i + 1).append("<td>已取消</td>");
            } else if (data[i].status == "6") {
                $(confTableId).find("tr").eq(i + 1).append("<td>已结束</td>");
            }
        }
    }
    $(confTableId).initDataTable({
        rowSelected: true,
        data: data
    });
}

/**
 * 创建相关会议列表
 */
function createDetailedConfTable(data) {
    var confData = data;
    if ($(detailedConfTable).find("li") != null) {
        $(detailedConfTable).find("li").remove();
    }
    if (confData != null) {
        var flag = false;
        for (var i = 0; i < confData.length; i++) {
            if (confData[i].status == 2 || confData[i].status == 4) {
                flag = true;
                break;
            }
        }
        for (var i = 0; i < confData.length; i++) {
            $(detailedConfTable).append("<li class='middle_2_top_2_li'></li>");
            $(detailedConfTable).find("li").eq(i).append("<div class='middle_2_top_list'></div>");
            switch (confData[i].status) {
                case "1":
                    $(detailedConfTable).find("li").eq(i).find(".middle_2_top_list").append("<div class='middle_2_top_list_1_space'></div><div class='middle_2_top_list_1_block'></div><div class='middle_2_top_list_1'></div>");
                    $(detailedConfTable).find("li").eq(i).find(".middle_2_top_list_1_block").addClass("calendar-book");
                    $(detailedConfTable).find("li").eq(i).find(".middle_2_top_list_1").addClass("calendar-book-color");
                    break;
                case "2":
                    $(detailedConfTable).find("li").eq(i).find(".middle_2_top_list").append("<div class='middle_2_top_list_1_space'></div><div class='middle_2_top_list_1_block'></div><div class='middle_2_top_list_1'></div>");
                    $(detailedConfTable).find("li").eq(i).find(".middle_2_top_list_1_block").addClass("calendar-approval");
                    $(detailedConfTable).find("li").eq(i).find(".middle_2_top_list_1").addClass("calendar-approval-color");
                    break;
                case "3":
                    $(detailedConfTable).find("li").eq(i).find(".middle_2_top_list").append("<div class='middle_2_top_list_1_space'></div><div class='middle_2_top_list_1_block'></div><div class='middle_2_top_list_1'></div>");
                    $(detailedConfTable).find("li").eq(i).find(".middle_2_top_list_1_block").addClass("calendar-important-book");
                    $(detailedConfTable).find("li").eq(i).find(".middle_2_top_list_1").addClass("calendar-important-book-color");
                    break;
                case "4":
                    $(detailedConfTable).find("li").eq(i).find(".middle_2_top_list").append("<div class='middle_2_top_list_1_space'></div><div class='middle_2_top_list_1_block'></div><div class='middle_2_top_list_1'></div>");
                    $(detailedConfTable).find("li").eq(i).find(".middle_2_top_list_1_block").addClass("calendar-important-approval");
                    $(detailedConfTable).find("li").eq(i).find(".middle_2_top_list_1").addClass("calendar-important-approval-color");
                    break;
                case "5":
                    $(detailedConfTable).find("li").eq(i).find(".middle_2_top_list").append("<div class='middle_2_top_list_1_space'></div><div class='middle_2_top_list_1_block'></div><div class='middle_2_top_list_1'></div>");
                    $(detailedConfTable).find("li").eq(i).find(".middle_2_top_list_1_block").addClass("calendar-cancel");
                    $(detailedConfTable).find("li").eq(i).find(".middle_2_top_list_1").addClass("calendar-cancel-color");
                    break;
                case "6":
                    $(detailedConfTable).find("li").eq(i).find(".middle_2_top_list").append("<div class='middle_2_top_list_1_space'></div><div class='middle_2_top_list_1_block'></div><div class='middle_2_top_list_1'></div>");
                    $(detailedConfTable).find("li").eq(i).find(".middle_2_top_list_1_block").addClass("calendar-finish");
                    $(detailedConfTable).find("li").eq(i).find(".middle_2_top_list_1").addClass("calendar-finish-color");
                    break;
                default:
                    break;
            }
            $(detailedConfTable).find("li").eq(i).find(".middle_2_top_list_1").append(confData[i].meetingName);
            $(detailedConfTable).find("li").eq(i).find(".middle_2_top_list").append("<div class='middle_2_top_list_2'></div>");
            $(detailedConfTable).find("li").eq(i).find(".middle_2_top_list_2").append("会议时间:&nbsp;" + confData[i].conferenceTime + "&nbsp;&nbsp;&nbsp;&nbsp;会议级别:&nbsp;" + confData[i].flag + "<br/>" + "会议地点:&nbsp;" + confData[i].boardroom + "&nbsp;&nbsp;&nbsp;&nbsp;参会人数:&nbsp;" + confData[i].stafferCount);
            if (confSelectedData != null) {
                if (confData[i].id == confSelectedData.id) {
                    $(detailedConfTable).find("li").eq(i).addClass("middle_2_top_list_1_selected");
                }
            }
            $(detailedConfTable).find("li").eq(i).find(".middle_2_top_list").mouseenter(function () {
                $(this).addClass("hover");
            });
            $(detailedConfTable).find("li").eq(i).find(".middle_2_top_list").mouseleave(function () {
                $(this).removeClass("hover");
            });
            $(detailedConfTable).find("li").eq(i).find(".middle_2_top_list").click([confData[i], flag], function (event) {
                confData = event.data[0];
                flag = event.data[1];
                var value;
                if (confData.status == "1" || confData.status == "3") {
                    if (confData.status == "1") {
                        value = "2";
                    } else {
                        value = "4";
                    }
                    $(approvalDialog).initCustomDialog({
                        title: "审批会议",
                        width: 600,
                        height: 580,
                        button1: "批准会议",
                        data1: confData.id + ":" + value,
                        disabled1: flag,
                        button2: "撤销批准",
                        disabled2: true,
                        button3: "拒绝会议",
                        data3: confData.id + ":" + "5",
                        disabled3: false,
                        buttonSize: 3,
                        auto: false,
                        modal: true
                    });
                } else if (confData.status == "2" || confData.status == "4") {
                    if (confData.status == "2") {
                        value = "1";
                    } else {
                        value = "3";
                    }
                    $(approvalDialog).initCustomDialog({
                        title: "审批会议",
                        width: 600,
                        height: 580,
                        button1: "批准会议",
                        disabled1: true,
                        button2: "撤销批准",
                        data2: confData.id + ":" + value,
                        disabled2: false,
                        button3: "拒绝会议",
                        data3: confData.id + ":" + "5",
                        disabled3: false,
                        buttonSize: 3,
                        auto: false,
                        modal: true
                    });
                } else {
                    $(approvalDialog).initCustomDialog({
                        title: "审批会议",
                        width: 600,
                        height: 580,
                        button1: "批准会议",
                        disabled1: true,
                        button2: "撤销批准",
                        disabled2: true,
                        button3: "拒绝会议",
                        disabled3: true,
                        buttonSize: 3,
                        modal: true
                    });
                }
                $(approvalDialog).openCustomDialog();
                $(approvalDialog).find("#meetingname").val(confData.meetingName);
                $(approvalDialog).find("#boardroom").val(confData.boardroom);
                $(approvalDialog).find("#meetingtime").val(confData.conferenceTime);
                $(approvalDialog).find("#project").val(confData.project);
                $(approvalDialog).find("#flag").val(confData.flag);
                $(approvalDialog).find("#secretlevel").val(confData.secretLevel);
                $(approvalDialog).find("#originator").val(confData.originator.stafferName);
                $(approvalDialog).find("#telephone").val(confData.originator.telphone);
                $(approvalDialog).find("#moderator").val(confData.moderator);
                $(approvalDialog).find("#staffercount").val(confData.stafferCount);
                $(approvalDialog).find("#staffer").val(confData.staffer);
                $(approvalDialog).find("#device").val(confData.device);
                $(approvalDialog).find("#remark").val("");
                $(approvalDialog).find("#remarkWarning").hide();
            });
        }
    }
}

/**
 * 获取列表选中行
 */
function getSelectedRow(id, confId) {
    var count = $(id).find(".middle_2_bottom_table_1").length;
    for (var i = 0; i < count; i++) {
        if (confId == $(id).find(".middle_2_bottom_table_1").eq(i).find("td").eq(0).find("input").val()) {
            row = i + 1;
            return row;
        }
    }
}

/**
 * 设置会议室默认状态
 */
function refreshConfTable() {
    bookedSelectedRow = null;
    confSelectedRow = null;
    bookedSelectedData = null;
    confSelectedData = null;

    // 刷新待审批会议列表
    $(bookedConfTableId).setSelectedRow({
        rowNumber: bookedSelectedRow
    });

    // 刷新会议显示列表
    data = getConfInfo(confTableUrl);
    createConfTable(data);
    $(confTableId).setSelectedRow({
        rowNumber: confSelectedRow
    });

    // 刷新会议日历
    $(confCalendarId).drewWeekCalendar({data: data});

    // 刷新相关会议列表
    if ($(detailedConfTable).find("li") != null) {
        $(detailedConfTable).find("li").remove();
    }
}

/**
 * 获取起始时间
 */
function getStartTime(dt) {
    var startTime = dt;
    var day = dt.getDay();
    switch (day) {
        case 0:
            startTime.setDate(startTime.getDate() - 6);
            break;
        case 2:
            startTime.setDate(startTime.getDate() - 1);
            break;
        case 3:
            startTime.setDate(startTime.getDate() - 2);
            break;
        case 4:
            startTime.setDate(startTime.getDate() - 3);
            break;
        case 5:
            startTime.setDate(startTime.getDate() - 4);
            break;
        case 6:
            startTime.setDate(startTime.getDate() - 5);
            break;
        default:
            break;
    }
    return formatDate(startTime) + " 00:00:00";
}

/**
 * 获取终止时间
 */
function getEndTime(dt) {
    var endTime = dt;
    var day = dt.getDay();
    switch (day) {
        case 1:
            endTime.setDate(endTime.getDate() + 6);
            break;
        case 2:
            endTime.setDate(endTime.getDate() + 5);
            break;
        case 3:
            endTime.setDate(endTime.getDate() + 4);
            break;
        case 4:
            endTime.setDate(endTime.getDate() + 3);
            break;
        case 5:
            endTime.setDate(endTime.getDate() + 2);
            break;
        case 6:
            endTime.setDate(endTime.getDate() + 1);
            break;
        default:
            break;
    }
    return formatDate(endTime) + " 23:59:59";
}

/**
 * 格式化日期
 */
function formatDate(dt) {
    var curYear = dt.getFullYear();
    var curMonth = (dt.getMonth() + 1);
    if (curMonth < 10) {
        curMonth = "0" + curMonth;
    }
    var curDay = dt.getDate();
    if (curDay < 10) {
        curDay = "0" + curDay;
    }
    return curYear + "-" + curMonth + "-" + curDay;
}

/**
 * 格式化时间(HH:mm)
 */
function formatTime(dt) {
    var curHour = dt.getHours();
    if (curHour < 10) {
        curHour = "0" + curHour;
    }
    var curMintues = dt.getMinutes();
    if (curMintues < 10) {
        curMintues = "0" + curMintues;
    }
    var curSecond = dt.getSeconds();
    if (curSecond < 10) {
        curSecond = "0" + curSecond;
    }
    return curHour + ":" + curMintues;
}